<?php
/*
* Class teia\webdev\php\core\
* Core 
*
* @author Bernardo Quibiana
* @date created: 20111122
*
* @date modified: 20111122
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

class HtmlUtil {
	
	static function generate_select($options, $selected, $onchange=NULL, $name='_select', $size=1, $multiple=FALSE) {
		$action = is_null($onchange) ? "": " onchange=\"$onchange\"";
		$show_mult = $multiple ? 'multiple="mutiple"' : '';
		if (is_null($selected)) $selected = key($options);
		$content = "<select name=\"$name\" size=\"$size\" $show_mult $action>\n";
		foreach ($options as $value => $txt) {
			if ($value == $selected) {
				$content .= "\t<option value=\"$value\" selected=\"selected\">$txt</option>\n";
			} else {
				$content .= "\t<option value=\"$value\">$txt</option>\n";
			}
		}
		$content .= "</select>\n";
		return $content;
	}
	
	static function display_rating_form($var_array=NULL) {
		$content = '
	<form action="">
        <div class="stars">
          <label for="rating-1"><input id="rating-1" name="rating" type="radio" value="1">0.5 Stars</label>
          <label for="rating-2"><input id="rating-2" name="rating" type="radio" value="2">1 Star</label>
          <label for="rating-3"><input id="rating-3" name="rating" type="radio" value="3">1.5 Stars</label>
          <label for="rating-4"><input id="rating-4" name="rating" type="radio" value="4">2 Stars</label>
          <label for="rating-5"><input id="rating-5" name="rating" type="radio" value="5">2.5 Stars</label>
          <label for="rating-6"><input id="rating-6" name="rating" type="radio" value="6">3 Stars</label>
          <label for="rating-7"><input id="rating-7" name="rating" type="radio" value="7">3.5 Stars</label>
          <label for="rating-8"><input id="rating-8" name="rating" type="radio" value="8">4 Stars</label>
        </div>
        <div>
          <!--input type="submit" value="Enviar"-->
      	</div>
  	</form>';
		
		echo $content;
	}
	
	/**
	* Display a specified template by importing the PHP file
	* @param string $url the url pointing to the template PHP file
	* @param string[] $var_array associative string array containing the values for variables used in the template
	*/
	static function display_template($url, $var_array=NULL) {
		if ($var_array && is_array($var_array)) {
			foreach ($var_array as $key => $value) {
				$$key = $value;
				//echo "DEBUG: FunctionPool::display_template() >>\t$key = $value";
			}
		}
		require $url;
	}
	
	/**
	 * Display user log in form using the template login_form_inc
	 * @param string[] $txt_array associative string array containing the text for displayble form field names
	 */
	static function display_login_form($txt_array=NULL) {
		self::display_template('login_form.inc', $txt_array);
		//require 'login_form.inc';
	}
	
	/**
	 * Display user sign-up form using the template signup_form_inc
	 * @param string[] $txt_array associative string array containing the text for displayble form field names
	 */
	static function display_signup_form($txt_array=NULL) {
		self::display_template('signup_form.inc', $txt_array);
	}
	
	/**
	 * Display user navigation message using the template usr_nav_msg.inc
	 * @param string[] $txt_array associative string array containing the values for variables used in the template
	 */
	static function display_usr_nav_msg($txt_array=NULL) {
		self::display_template('usr_nav_msg.inc', $txt_array);
	}
	
	static function display_pwd_req_form($txt_array=NULL) {
		self::display_template('pwd_recaptcha_form.inc', $txt_array);
	}
	
	/*
	 * HTML templates
	*/
	
	static function include_jquery($url=NULL) {
		if ($url && is_file($url)) {
			echo "<script type=\"text/javascript\" src=\"$url\"></script>";
		} else {
		echo "<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-latest.pack.js\"></script>\n";
		}
	}
	
	
	/**
	* Write keywords using the meta element
	 * @param array $keywords string array of keywords
	 */
	 static function display_meta_keywords($keywords) {
	$keywords_str = self::array_to_str($keywords, ', ');
	echo '<meta name="keywords" content="' . $keywords_str . '" />\n';
	}
	
		/**
		* Write the page description using the meta element (should be less than 155 chars, relevant to search engines!)
	* @param string $description description of the page content in plain text
	*/
	static function display_meta_description($description) {
	echo '<meta name="description" content="' . $description . '" />\n';
	}
	
	/**
	* Write the search engine robot options using the meta element
	* @param array $values
	*/
	static function display_meta_robots($values) {
	$robots_str = self::array_to_str($values, ',');
	echo '<meta name="robots" content="' . $robots_str. '" />\n';
		}
		
		/**
	* Write the author name using the meta element
	* @param string $author name of the web developer in plain text
	*/
	static function display_meta_author($author='Teia - Web Development') {
	echo '<meta name="author" content="' . $author. '" />\n';
		}
		
		static function display_meta_charset($charset='UTF-8') {
	echo '<meta charset="' . $charset. '" />\n';
	}
	
		static function display_meta($var_array) {
		if ($var_array && is_array($var_array)) {
		foreach ($var_array as $key => $value) {
		$$key = $value;
		}
		}
	
		if (!isset($author)) $author = 'Teia - Web Development';
		self::display_meta_author($author);
			
			if (isset($description)) self::display_meta_description($description);
		if (isset($keywords)) self::display_meta_keywords($keywords);
		if (isset($robots)) self::display_meta_robots($robots);
		if (!isset($charset)) $charset = 'UTF-8';
		self::display_meta_charset($charset);
	}
	
		/**
		* Include AddThis toolbar, from www.addthis.com, for sharing in Facebook, Tweeter and others
		* @param int $style_num style number (three styles are available)
		*/
		static function include_addthis_toolbar($style_num=1) {
		echo "<!-- AddThis Button BEGIN -->";
		switch ($style_num) {
		case 1:
					echo 
	"<div class=\"addthis_toolbox addthis_default_style \">
		<a class=\"addthis_button_preferred_1\"></a>
		<a class=\"addthis_button_preferred_2\"></a>
		<a class=\"addthis_button_preferred_3\"></a>
		<a class=\"addthis_button_preferred_4\"></a>
		<a class=\"addthis_button_compact\"></a>
		<a class=\"addthis_counter addthis_bubble_style\"></a>
	</div>
	<script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4ea870e5298c5b10\"></script>";
					break;
				case 2:
					echo 
	"<div class=\"addthis_toolbox addthis_default_style addthis_32x32_style\">
		<a class=\"addthis_button_preferred_1\"></a>
		<a class=\"addthis_button_preferred_2\"></a>
		<a class=\"addthis_button_preferred_3\"></a>
		<a class=\"addthis_button_preferred_4\"></a>
		<a class=\"addthis_button_compact\"></a>
		<a class=\"addthis_counter addthis_bubble_style\"></a>
	</div>
	<script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4ea871344e65747b\"></script>";
					break;	
				default:
					echo 
	"<div class=\"addthis_toolbox addthis_default_style \">
		<a class=\"addthis_button_facebook_like\" fb:like:layout=\"button_count\"></a>
		<a class=\"addthis_button_tweet\"></a>
		<a class=\"addthis_button_google_plusone\" g:plusone:size=\"medium\"></a>
		<a class=\"addthis_counter addthis_pill_style\"></a>
	</div>
	<script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4ea871520a7cb528\"></script>";
					break;
			}
			echo "<!-- AddThis Button END -->";
		}
}
?>